﻿using Feature.YiGuo.Framework.Contract;
using Feature.YiGuo.Framework.Utility.Extensions;
using Feature.YiGuo.Framework.Utility.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace ApiGateway.Filters
{
    /// <summary>
    /// 签名验证标签
    /// </summary>
    public class SignValidationAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var request = actionContext.Request;

            //var key = (actionContext.ActionArguments.Keys.Contains("request") ? "request" : actionContext.ActionArguments.Keys.FirstOrDefault()) ?? "request";

            //var isContainsRequest = actionContext.ActionArguments.TryGetValue(key, out request);

            //if (!isContainsRequest || request == null)
            //{
            //    CreateResponseMessage(actionContext, "参数无效");
            //    return;
            //}

            //将所有参数转换成字典 且首字母小写

            string appId = this.GetHeaderValue(request, "appId");

            if (string.IsNullOrEmpty(appId))
            {
                CreateResponseMessage(actionContext, "无效的应用号");
                return;
            }

            //var appSecret = AppUserMap.Users[appId];

            //if (string.IsNullOrEmpty(appSecret))
            //{
            //    CreateResponseMessage(actionContext, "无效的应用号");
            //    return;
            //}

            string timeStamp = this.GetHeaderValue(request, "timeStamp");
            
            if (string.IsNullOrEmpty(timeStamp))
            {
                CreateResponseMessage(actionContext, "请求时间超时");
                return;
            }
            var minutes = Math.Abs((DateTime.Now - timeStamp.ConvertToDate()).TotalMinutes);
            var isTimeout = minutes > 0 && minutes < 10;
            if (!isTimeout)
            {
                CreateResponseMessage(actionContext, "请求时间超时" + (DateTime.Now).GetTimeStamp());
                return;
            }

            string signature= this.GetHeaderValue(request, "signature");

            if (string.IsNullOrEmpty(signature))
            {
                CreateResponseMessage(actionContext, "签名无效");
                return;
            }

            //var sign = SignatureHelper.GetSignature(parameters, appSecret);

            //if (!sign.Equals(signature))
            //{
            //    CreateResponseMessage(actionContext, "签名无效");
            //    return;
            //}

            base.OnActionExecuting(actionContext);
        }

        private string GetHeaderValue(HttpRequestMessage request,string key)
        {
            IEnumerable<string> values =null;
            if (request.Headers.TryGetValues(key, out values))
            {
                return values.FirstOrDefault();
            }

            return "";
        }

        private void CreateResponseMessage(HttpActionContext actionContext, string msg)
        {
            var result = new Response
            {
                Code = "-1",
                Message = msg,
                IsSuccessful = false
            };

            // 重新打包回传的讯息
            actionContext.Response = actionContext.Request.CreateResponse(result);
        }
    }
}